package com.wlyuan.open.scanner.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlyuan.open.scanner.constant.Constants;
import com.wlyuan.open.scanner.dao.entity.WaybillDO;
import com.wlyuan.open.scanner.dao.mapper.WaybillMapper;
import com.wlyuan.open.scanner.dao.write.WaybillWriteMapper;
import com.wlyuan.open.scanner.domain.transport.relation.WayBill;
import com.wlyuan.open.scanner.domain.transport.relation.WayBillConvert;
import com.wlyuan.open.scanner.repository.WayBillRepository;
import com.wlyuan.open.scanner.utils.enums.order.OrderSrcClassEnum;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Service(value = "wayBillRepository")
@Slf4j
@RequiredArgsConstructor
public class WayBillRepositoryImpl implements WayBillRepository {
    private final WaybillMapper waybillMapper;
    private final WaybillWriteMapper waybillWriteMapper;

    @Override
    public WayBill getByUp(Long id) {
        LambdaQueryWrapper<WaybillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WaybillDO::getUp, id);
        var data = waybillMapper.selectOne(queryWrapper);
        return WayBillConvert.convert(data);
    }

    @Override
    public WayBill getByDown(Long id) {
        LambdaQueryWrapper<WaybillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WaybillDO::getDown, id);
        var data = waybillMapper.selectOne(queryWrapper);
        return WayBillConvert.convert(data);
    }

    @Override
    public List<WayBill> list(Integer pageNum) {
        //(a.Index_SrcClass = 1 and a.Index_SrcOrderID = 0 and a.Index_Status <> -1) or (a.Index_SrcClass = 2 and a.Index_Status <> -1)
        var data = waybillMapper.list(pageNum, Constants.PAGE_SIZE);
        return WayBillConvert.convert(data);
    }

    @Override
    public List<Long> getByTime(String startTime, String endTime) {
        return waybillMapper.getByTime(startTime, endTime);
    }

    @Override
    public List<Long> getByTenantIdTime(Long tenantId, String startTime, String endTime) {
        return waybillMapper.getByTenantIdTime(tenantId, startTime, endTime);
    }

    @Override
    public List<WayBill> list(Integer pageNum, Long tenantId) {
        //(a.Index_SrcClass = 1 and a.Index_SrcOrderID = 0 and a.Index_Status <> -1) or (a.Index_SrcClass = 2 and a.Index_Status <> -1)
        var data = waybillMapper.listByTenantId(pageNum, Constants.PAGE_SIZE, tenantId);
        return WayBillConvert.convert(data);
    }

    @Override
    public Integer count() {
        return waybillMapper.count();
    }

    @Override
    public Integer count(Long tenantId) {
        return waybillMapper.countByTenantId(tenantId);
    }

    @Override
    public void init() {
        logger.info("truncate");
        waybillWriteMapper.truncate();
        logger.info("insert");
        waybillWriteMapper.init();
        logger.info("update");
        waybillWriteMapper.update();
        logger.info("初始化完成");
    }

    @Override
    public void initTime(String startTime, String endTime) {
        logger.info("insert");
        waybillWriteMapper.initTime(startTime, endTime);
        logger.info("update");
        waybillWriteMapper.update();
        logger.info("初始化完成");
    }

    @Override
    public void save(Long orderId, Long tenantId, Long srcOrderId, Long srcClass) {
        //客户单
        if (OrderSrcClassEnum.isOrder(srcClass)) {
            //是否原始客户单
            if (LongUtils.notNullLong(srcOrderId)) {
                LambdaQueryWrapper<WaybillDO> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(WaybillDO::getUp, srcOrderId);
                var data = waybillMapper.selectOne(queryWrapper);
                //是否存在
                if (Optional.ofNullable(data).isPresent()) {
                    data.setDown(orderId);
                    data.setUpdateTime(LocalDateTime.now());
                    waybillWriteMapper.updateById(data);
                } else {
                    save(srcOrderId, orderId, tenantId);
                }
            } else {
                //原始订单
                save(orderId, 0L, tenantId);
            }
        }
        //运输单
        if (OrderSrcClassEnum.isTransport(srcClass)) {
            save(orderId, 0L, tenantId);
        }
    }

    private void save(Long up, Long down, Long tenantId) {
        WaybillDO waybillDO = new WaybillDO();
        waybillDO.setUp(up);
        waybillDO.setDown(down);
        waybillDO.setTenantId(tenantId);
        waybillDO.setCreateTime(LocalDateTime.now());
        waybillWriteMapper.insert(waybillDO);
    }

}
